<?php
include_once("../../../common/modules/checkA.php");
include_once("../../../../config.php");
?>


<?


//print_r2($_REQUEST);

//获取当前优惠券的信息

$coupon=select_one_line_all_items($table_prefix."coupon","nid = '".$_REQUEST['coupon_nid']."'");

//如果选择优惠券，就提示没有优惠券
if(!$coupon['nid'])
{
    ?>
    <script type="text/javascript">
        alert("选择优惠券失败:未选择优惠券");
        document.location.href = "coupon-send.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&nid=<? echo $_REQUEST['coupon_nid'] ?>&sys_part=<? echo $system_part ?>&tag=<? echo $_REQUEST['tag'] ?>"
    </script>

    <?php
    exit;
}


//优惠券数量，要大于0
if($_REQUEST['coupon_num'] < 1)
{
    ?>
    <script type="text/javascript">
        alert("派发优惠券数量不能为0");
        document.location.href = "coupon-send.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&nid=<? echo $_REQUEST['coupon_nid'] ?>&sys_part=<? echo $system_part ?>&tag=<? echo $_REQUEST['tag'] ?>"
    </script>

    <?php
    exit;
}

//print_r2($coupon);



//如果没有选中用户，就提示没有派券成功
if(!$_REQUEST['member'])
{
?>
    <script type="text/javascript">
        alert("派券失败:未选择用户");
        document.location.href = "coupon-send.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&coupon_nid=<? echo $_REQUEST['coupon_nid'] ?>&sys_part=<? echo $system_part ?>&tag=<? echo $_REQUEST['tag'] ?>"
    </script>

<?php
exit;
}


//所有参数正常，开始派发优惠券。
else
{

foreach ($_REQUEST['member'] as $k=>$v)
{

    echo "<br/>".$v."应该要有一张这个的优惠券<br/>";


    $insert_data                   = [];
    $insert_data['ai_name']        = $coupon['name'];//优惠券名字
    $insert_data['ai_remark']      = $coupon['remark'];//优惠券备注
    $insert_data['ai_coupon_type'] = $coupon['coupon_type'];//优惠券类型
    $insert_data['ai_is_work']     = "1";//优惠券是否激活


    //这个优惠券是折扣还是定额
    if ($coupon['coupon_type'] == 10 || $coupon['coupon_type'] == 40 ) {
        $insert_data['ai_reduce_price'] = $coupon['reduce_price'];
    } else {
        $insert_data['ai_discount'] = $coupon['discount'];
    }


    //这里应该要根据优惠券的到期类型去到做判断。到底是+日数，还是固定到期时间。
    if($coupon['end_time']>0)
    {
        $expire_day=$coupon['end_time'];
    }
    else
    {
        $expire_day=date("Y-m-d H:i:s",strtotime("+".$coupon['expire_day']." day "));
    }


$insert_data['ai_end_time']  = strtotime($expire_day);
$insert_data['ai_user_id']     = $v;
$insert_data['ai_coupon_id']   = $coupon['nid'];
$insert_data['ai_is_free']     = $coupon['is_free'];
$insert_data['ai_create_time'] = time();//派发时间



if($insert_data['ai_coupon_id'])
{

    for ($i=1; $i<=$_REQUEST['coupon_num']; $i++)
    {
            $sql = insert_date($table_Prefix . 'coupon_member', $insert_data);
        $query = do_query($sql) or die ("error:" . mysqli_error());

    }


    $_member        = select_one_line_all_items($table_prefix . "member", "nid = '" . $v . "'");
    $_wechat_member = select_one_line_all_items($table_prefix . "wechat_member", "unionid = '" . $_member['unionid'] . "'");


    //触发发券通知信息
    //curl 一个api地址，带上通知内容代号，被通知人，然后就触发了。
    echo $_url=$domain."/api/wechat_message/notice_coupon_sent.php?openid=".$_wechat_member['openid'];
    //有关注公众号，才发送通知。
    if($_wechat_member['openid'])
    {
        file_get_contents($_url);
    }

}


}


}




?>


<script type="text/javascript">
     alert("输入信息已保存，点击确认返回继续操作.");
     document.location.href = "detail-list.php?sys_part=coupon"
</script>


